package org.nci.platform.workflow.config;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@ConditionalOnWebApplication
@AutoConfigureAfter(WebMvcAutoConfiguration.class)
public class VueAutoConfiguration implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // 将根路径和所有非静态资源路径映射到index.html
        registry.addViewController("/workflow-ui/").setViewName("forward:index.html");
    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 映射 Vue 静态资源
        registry.addResourceHandler("/workflow-ui/**")
                .addResourceLocations("classpath:/META-INF/dist/")
                .setCachePeriod(3600)
                .resourceChain(true);
        // 映射 Vue 静态资源
        registry.addResourceHandler("/workflow-ui/css/**")
                .addResourceLocations("classpath:/META-INF/dist/workflow-ui/css/")
                .setCachePeriod(3600)
                .resourceChain(true);
        // 映射 Vue 静态资源
        registry.addResourceHandler("/workflow-ui/fonts/**")
                .addResourceLocations("classpath:/META-INF/dist/workflow-ui/fonts/")
                .setCachePeriod(3600)
                .resourceChain(true);
        // 映射 Vue 静态资源
        registry.addResourceHandler("/workflow-ui/img/**")
                .addResourceLocations("classpath:/META-INF/dist/workflow-ui/img/")
                .setCachePeriod(3600)
                .resourceChain(true);
        // 映射 Vue 静态资源
        registry.addResourceHandler("/workflow-ui/js/**")
                .addResourceLocations("classpath:/META-INF/dist/workflow-ui/js/")
                .setCachePeriod(3600)
                .resourceChain(true);
    }
}